<?php
include_once("Task.php");

class TaskManager {
	public $tasks = array();
	public $network;
	public $pm;

	function __construct($network) {
		$this->pm = new ParamManager();
		$this->network = $network;

		$data = "cmd=task_list\n";
		$this->network->send($data);
		while($from = $this->network->recv()) {
			if(strpos($from, "task_list_reply") !== false) {
				break;
			}
		}

		$this->pm = new ParamManager();
		$this->pm->init($from, "|");
		$count = $this->pm->get("count");
		for($index = 0; $index < $count; $index ++) {
			$from = $this->network->recv();
			if(strpos($from, "task_list_item") !== false) {
				$this->pm->init($from, "|");
				$task = new Task($this->pm->get("id"), $this->network);
				$task->priority = $this->pm->get("priority");
				$task->state = $this->pm->get("state");
				$task->timestamp = $this->pm->get("timestamp");
				$task->todo = $this->pm->get("todo");
				$task->complete = $this->pm->get("complete");
				$task->total = $this->pm->get("total");
				$task->seconds = $this->pm->get("seconds");
				$this->tasks[] = $task;
			}
			else {
				// must be something else, like 'task_list_reply' or 'notice', ignore
				$index --;
			}
		}
	}

	function add() {
		global $settings;

		// add new task
		$data = "cmd=task_add\n";
		$this->network->send($data);
		while($from = $this->network->recv()) {
			if(strpos($from, "task_add_reply") !== false) {
				break;
			}
		}
		$this->pm->init($from, "|");
		$task_id = $this->pm->get("id");
		
		$task = new Task($task_id, $this->network);

		// send over some default task data
		$task->set("db_host", $settings["default"]["db_host"]);
		$task->set("db_port", $settings["default"]["db_port"]);
		$task->set("db_name", $settings["default"]["db_name"]);
		$task->set("db_user", $settings["default"]["db_user"]);
		$task->set("db_pass", $settings["default"]["db_pass"]);

		$task->set("todo_count", "1");
		$task->set("block_size", "1");
		$task->set("timeout", "30");

		return new Task($task_id, $this->network);
	}
}

?>
